﻿using System;
using System.Linq;
using System.Transactions;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;

namespace iss.sg4210.se18.team6s.jass.BL
{
    public class PublishingsFacade
    {
        IEvents eventsRepo = null;
        IAddresses addressRepo = null;
        IArticles articlesRepo = null;

        public PublishingsFacade()
        {
            eventsRepo = DataAccessFactory.Instance.Event;
            articlesRepo = DataAccessFactory.Instance.Article;
            addressRepo = DataAccessFactory.Instance.Address;
        }

        #region Events
        public void AddEvent(Event eventObj, Address address)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    //AddAddress(address);
                    //get address id and set to eventsObj
                    eventObj.Address = address;
                    eventsRepo.AddEvent(eventObj);
                }
                catch (Exception ex)
                {
                    throw new JASSException(JASSConstants.JASS_ERROR_SAVE, ex);
                }
                ts.Complete();
            }
        }

        public IQueryable<Event> FindAllEventsByUser(Guid userId)
        {
            try
            {
                return eventsRepo.FindAllEventsByUser(userId);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public IQueryable<Event> FindAllEvents()
        {
            try
            {
                return eventsRepo.FindAllEvents();
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public IQueryable<Event> FindAllLatestEvents()
        {
            try
            {
                return eventsRepo.FindAllEvents().Where(m => DateTime.Compare(m.Date, DateTime.Today) >= 0);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }


        public Event GetEvent(int id)
        {
            try
            {
                return eventsRepo.GetEvent(id);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public void UpdateEvent(Event events, Address address)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    UpdateAddress(address);
                    eventsRepo.UpdateEvent(events);
                }
                catch (Exception ex)
                {
                    throw new JASSException(JASSConstants.JASS_ERROR_UPDATE, ex);
                }
                ts.Complete();
            }
        }

        public void DeleteEvent(int id)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    Event eve = GetEvent(id);
                    eventsRepo.DeleteEvent(id);
                    DeleteAddress(eve.AddressId);
                }
                catch (Exception ex)
                {
                    throw new JASSException(JASSConstants.JASS_ERROR_DELETE, ex);
                }
                ts.Complete();
            }
        }
        #endregion

        #region Article
        public IQueryable<Article> FindAllArticlesByUser(Guid userId)
        {
            try
            {
                return articlesRepo.FindAllArticlesByUser(userId);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public IQueryable<Article> FindAllArticles()
        {
            try
            {
                return articlesRepo.FindAllArticles();
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public IQueryable<Article> FindAllLatestArticles()
        {
            try
            {
                return articlesRepo.FindAllArticles().Where(m => DateTime.Compare(m.PublishingDate, DateTime.Today) >= 0);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public Article GetArticle(int id)
        {
            try
            {
                return articlesRepo.GetArticle(id);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public void AddArticle(Article article)
        {
            try
            {
                articlesRepo.AddArticle(article);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_SAVE, ex);
            }
        }

        public void UpdateArticle(Article article)
        {
            try
            {
                articlesRepo.UpdateArticle(article);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_UPDATE, ex);
            }
        }

        public void DeleteArticle(int id)
        {
            try
            {
                articlesRepo.DeleteArticle(id);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_DELETE, ex);
            }
        }
        #endregion

        #region Address
        public IQueryable<Address> FindAllAddresses()
        {
            try
            {
                return addressRepo.FindAllAddresses();
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public Address GetAddress(int id)
        {
            try
            {
                return addressRepo.GetAddress(id);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public void AddAddress(Address address)
        {
            try
            {
                addressRepo.AddAddress(address);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_SAVE, ex);
            }
        }

        public void UpdateAddress(Address address)
        {
            try
            {
                addressRepo.UpdateAddress(address);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_UPDATE, ex);
            }
        }

        public void DeleteAddress(int id)
        {
            try
            {
                addressRepo.DeleteAddress(id);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_DELETE, ex);
            }
        }
        #endregion



    }
}
